//
//  MyInforChangeNameViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MyInforChangeNameViewController.h"

@interface MyInforChangeNameViewController ()<UITextFieldDelegate>
/**  文本输入框  */
@property (strong, nonatomic) UITextField  *nameTextField;

@end

@implementation MyInforChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"修改姓名";
    self.rightButton.hidden = NO;
    [self.rightButton setTitle:@"保存" forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.nameTextField];
    
    self.nameTextField.sd_layout
    .topSpaceToView(self.view,LP_TopHeight + 20 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.view,15 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.view,15 * [UIScreen zj_screenWScale])
    .heightIs(44 * [UIScreen zj_screenHScale]);
}

/**  保存的方法  */
- (void)action_rightButton:(UIButton *)sender{
    __weak typeof(self) weakSelf = self;
    if (self.nameTextField.text.length == 0) {
        [YJProgressHUD showMessage:@"请输入修改的姓名" inView:self.view];
        return;
    }
    if ([self.nameTextField.text isEqualToString:self.name]) {
        [YJProgressHUD showMessage:@"修改姓名和原名字相同" inView:self.view];
        return;

    }
    [YJProgressHUD showMessage:@"修改中..." duration:10 view:self.view];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
   
    paramer[@"ACCOUNT_ID"] = ZJUSERINFORMODEL.ORG_ACCOUNT.ID;
    paramer[@"USER_NAME"] = self.nameTextField.text;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getUpdateUser] Parameter:paramer Success:^(id object) {
        NetWorkModel *mode = [NetWorkModel yy_modelWithJSON:object];
        if ([mode.STATE isEqualToString:@"20000"]) {
            if (weakSelf.block) {
                weakSelf.block(weakSelf.nameTextField.text);
            }
            [YJProgressHUD showSuccess:@"修改成功" inview:weakSelf.view];
            /**  更新用户信息  */
            [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.USER_NAME = weakSelf.nameTextField.text;
            
            [ZJUserInfoModel updateUserInfoToNSUserDefaults];
            
            /**  发送名字修改成功的通知 */
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LPChangeName" object:weakSelf userInfo:@{@"name": weakSelf.nameTextField.text}];
            [weakSelf.navigationController popViewControllerAnimated:YES];

        }else{
            [YJProgressHUD showMessage:@"修改失败" inView:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMessage:@"修改失败" inView:weakSelf.view];
    }];

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ( toBeString.length >= 50) {
        textField.text = [toBeString substringToIndex:50];
        return NO;
    }
    return YES;
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}
- (UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = ({
            UITextField *textField =  [UITextField new];
            textField.placeholder = @"请输入姓名";
            textField.font = [UIFont systemFontOfSize:17 * [UIScreen zj_textFontScale]];
            textField.text = self.name;
            [textField setTextFieldLeftforWidth:15 * [UIScreen zj_screenWScale]];
            textField.backgroundColor = [UIColor whiteColor];
            textField.textColor = [UIColor textBlackColor];
            textField.textAlignment = NSTextAlignmentLeft;
            textField.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
            textField.layer.borderWidth = 1;
            textField.layer.cornerRadius = 3;
            textField.layer.masksToBounds = YES;
            textField.delegate = self;
            textField.returnKeyType = UIKeyboardTypeTwitter;
            textField;
        });
    
    }
    return _nameTextField;
    
}
@end
